<?php
// 首页显示信息以及基本配置
namespace Partner\Controller;
use Think\Controller;
class UsersController extends CommonController{

 function users_list(){
     $id=session('id');
     $user_info=M('partner')->find($id);
     $bids=explode(',',$user_info['b_id']);
     $where=array();
     if($user_info['level']==1){
         $where['province_id']=$bids[0];
     }
     if($user_info['level']==2){
         $where['province_id']=$bids[0];
         $where['city_id']=$bids[1];
     }
     if($user_info['level']==3){
         $where['province_id']=$bids[0];
         $where['city_id']=$bids[1];
         $where['district_id']=$bids[2];

     }

     if($_GET['phone']){
         $where['phone']=$_GET['phone'];
     }
     if($_GET['province']>0){
         $where['province_id']=$_GET['province'];
     }

     if($_GET['city']>0){
         $where['city_id']=$_GET['city'];
     }
     if($_GET['district']>0){
         $where['district_id']=$_GET['district'];
     }
     if($_GET['min_price']||$_GET['max_price']){
         if($_GET['min_price']&&$_GET['max_price']){
             $where['_string']="count_price>=".$_GET['min_price']." and count_price<=".$_GET['max_price'];
         }elseif ($_GET['min_price']){
             $where['_string']="count_price>=".$_GET['min_price'];
         }else{
             $where['_string']="count_price<=".$_GET['max_price'];
         }

     }

     session('user_type',$_GET['type']);
     $user_type=session('user_type');

     if($user_type==2){
         $where['phone']=array('exp','is not null');
     }
     if($user_type==1){
         $where['phone']=array('exp','is null');
     }
     $users   =   D('Users');
     $count      = $users->where($where)->count();
     $this->assign('count',$count);
     $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
     $Page->setConfig('next','下一页');
     $Page->setConfig('prev','上一页');
     $show       = $Page->show();// 分页显示输出
     $users_list=$users
         ->where($where)
         ->limit($Page->firstRow.','.$Page->listRows)
         ->order('id desc')
         ->select();
     $users_lists=$users->select();
     $order=M('order');
     foreach ($users_lists as $key=>$value){
         if($value['phone']){
             $countorder_list=$order->where("buyer_id=".$value['id'].' and refund_state=0 and order_state>="20"')->select();
             $count_price=0;
             foreach ($countorder_list as $k=>$v){
                 $count_price=$count_price+$v['order_amount'];
             }
             $data['id']=$value['id'];
             $data['count_price']=$count_price;
             $users->save($data);
         }
     }
     $this->assign('page',$show);// 赋值分页输出
     $this->assign('users_list',$users_list);
     $this->assign('title','会员列表');
     $this->display();
 }
    function accountdetail(){
        $id=I('get.id');
        $users_log=M('users_log');
        $users=M('users');
        $user_info=$users->find($id);
        $count      = $users_log->where('uid='.$id)->count();
        $this->assign('count',$count);

        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $users_list=$users_log
            ->where('uid='.$id)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('users_list',$users_list);
        $this->assign('user_info',$user_info);

        $this->assign('title','会员积分列表');
        $this->display();

    }

    function xiaofeidetail(){
        $id=I('get.id');
        $order=M('order');
        session('order_type',$_GET['type']);
        $order_type=session('order_type');
        $where='';
        $count_where=' ';
        if($order_type==1){
            $where=' and refund_state=0 and order_state<"40" and order_state>"10" ';
        }
        if($order_type==2){
            $where=' and refund_state=0 and order_state>="40" ';
        }
        if($order_type==3){
            $where=' and refund_state>0';
        }
        if($order_type==4){
            $where=' and order_state="0"';
        }
        if($_GET['startDate']){

            $start_time=strtotime($_GET['startDate']);
            $where.=' and add_time>='.$start_time;
            $count_where.=' and add_time>='.$start_time;
        }
        if($_GET['endDate']){
            $end_time=strtotime($_GET['endDate']);
            $where.=' and add_time<='.$end_time;
            $count_where.=' and add_time<='.$end_time;
        }
        $countorder_list=$order->where("buyer_id=".$id.$count_where.' and refund_state=0 and order_state>="20" and order_type!=1')->select();
        $count_price=0;
        foreach ($countorder_list as $key=>$value){
            $count_price=$count_price+$value['order_amount'];
        }
        $count=$order->where("buyer_id=".$id.$where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $show       = $Page->show();// 分页显示输出
        $order_list=$order->where("buyer_id=".$id.$where) ->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('order_list',$order_list);
        $this->assign('order_type',$order_type);
        $this->assign('count_price',$count_price);
        $this->display();
    }
    function orderdetail(){
        $id=session('id');
        $user_info=M('partner')->find($id);
        $this->assign('order_edit',$user_info['order_edit']);
        $order_id=I('get.id');
        $order=M('order');
        $order_goods=M('order_goods');
        $order_info=$order->where('order_id='.$order_id)->find();
        $order_goods_info=$order_goods->where('order_id='.$order_id)->select();
        $order_price_action=M('order_price_action')->where('order_id='.$order_id)->order('action_id desc')->find();
        $this->assign('order_price_action',$order_price_action);
        $this->assign('order_goods_info',$order_goods_info);
        $this->assign('order_info',$order_info);
        $this->assign('title','订单详情');
        $this->display();
    }

    function angel(){
        $id =I('get.id');
        $p_id=session('id');
        $user_info=M('partner')->find($p_id);
        $list=M('area_angel')
            ->field('yt_users.phone,yt_area_angel.*')
            ->join('yt_partner on yt_partner.id=yt_area_angel.panter_id','left')
            ->join('yt_users on yt_partner.u_id=yt_users.id','left')
            ->where('yt_area_angel.uid='.$id)->select();

        $this->assign('user_info',$user_info);
        $this->assign('list',$list);
        $this->display();
    }
    function angel_list(){
        $id =I('get.id');
        $p_id=session('id');
        $user_info=M('partner')->find($p_id);
        $b_ids=explode(',',$user_info['b_id']);
        if($user_info['level']==1){
            $where='yt_area_angel.province_id='.$b_ids[0];
        }
        if($user_info['level']==2){
            $where='yt_area_angel.city_id='.$b_ids[1];
        }
        if($user_info['level']==3){
            $where='yt_area_angel.district_id='.$b_ids[2];

        }
        $list=M('area_angel')
            ->field('yt_users.phone,yt_area_angel.*')
            ->join('yt_users on yt_users.id=yt_area_angel.uid','left')
            ->where($where)->select();

        $this->assign('user_info',$user_info);
        $this->assign('list',$list);
        $this->display();
    }
    function add_angel(){
     $id =I('get.id');
     $userinfo=M('users')->find($id);
     $this->assign('userinfo',$userinfo);
     $this->display();
    }
    function edit_angel(){
        $id =I('get.id');
        $rs_info=M('area_angel')->find($id);
        $userinfo=M('users')->find($rs_info['uid']);
        $this->assign('userinfo',$userinfo);
        $this->assign('rs_info',$rs_info);
        $this->display();
    }

    function  edit_angel_post(){
        $id=session('id');
        $user_info=M('partner')->find($id);
        $data=I('post.');
        if($_POST){
            $info['uid']=$data['u_id'];
            $info['panter_id']=$id;
            $info['province_id']=$data['seleAreaNext'];
            $info['area_type']=3;
            if($data['seleAreaThird']>0){
                $info['city_id']=$data['seleAreaThird'];
                $info['area_type']=2;
            }
            if($data['seleAreaFouth']>0){
                $info['district_id']=$data['seleAreaFouth'];
                $info['area_type']=1;
            }


            $info['id']=$data['id'];
            $info['reason']=$data['reason'];
            $rs=M('area_angel')->save($info);
            if($rs){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }


        }
    }
    function add_angel_post(){
        $id=session('id');
        $user_info=M('partner')->find($id);
        $data=I('post.');
        if($_POST){
            $info['uid']=$data['u_id'];
            $info['panter_id']=$id;
            $info['province_id']=$data['seleAreaNext'];
            $info['area_type']=3;
            if($data['seleAreaThird']>0){
                $info['city_id']=$data['seleAreaThird'];
                $info['area_type']=2;
            }
            if($data['seleAreaFouth']>0){
                $info['district_id']=$data['seleAreaFouth'];
                $info['area_type']=1;
            }
            $info['status']=$user_info['level'];
            $c_info=$info;
            $c_info['status']=array('neq',5);
            $rs_info=M('area_angel')->where($c_info)->select();
            if($rs_info==null){
                $info['reason']=$data['reason'];
                $rs=M('area_angel')->add($info);
                if($rs){
                    $this->ajaxReturn(1);
                }else{
                    $this->ajaxReturn(0);
                }
            }else{
                $this->ajaxReturn(2);
            }

        }
    }

    function edit_angel_status(){
        if($_POST){
            $id=session('id');
            $data=I('post.');
            $info['id']=$data['id'];
            $info['status']=$data['data'];
            if($data['data']==-1){
                $info['jujue_id']=$id;
            }
            $rs=M('area_angel')->save($info);
            if($rs){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }
    }

}